package com.guomei.service.curse.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guomei.bean.curse.Course;
import com.guomei.bean.curse.CourseCategoryRelation;
import com.guomei.bean.curse.CreateOrUpdateCourseCategoryRelationDTO;
import com.guomei.mapper.curse.CourseCategoryRelationMapper;
import com.guomei.mapper.curse.CourseMapper;
import com.guomei.service.curse.CourseCategoryRelationService;
import com.guomei.utils.PageData;
import com.guomei.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程与类别关联表服务实现类
 */
@Service
@Slf4j
@Transactional
public class CourseCategoryRelationServiceImpl implements CourseCategoryRelationService {
    @Resource
    private CourseCategoryRelationMapper relationMapper;
    @Resource
    private CourseMapper courseMapper;

    @Override
    @Transactional
    public int addRelation(CourseCategoryRelation relation) {
        return relationMapper.insert(relation);
    }

    @Override
    @Transactional
    public int deleteRelationById(Long id) {
        return relationMapper.deleteById(id);
    }

    @Override
    @Transactional
    public int deleteRelationsByCourseId(Long courseId) {
        return relationMapper.deleteByCourseId(courseId);
    }

    @Override
    @Transactional
    public int deleteRelationsByCategoryId(Long categoryId) {
        return relationMapper.deleteByCategoryId(categoryId);
    }

    @Override
    @Transactional
    public int deleteRelationByCourseIdAndCategoryId(Long courseId, Long categoryId) {
        return relationMapper.deleteByCourseIdAndCategoryId(courseId, categoryId);
    }

    @Override
    public CourseCategoryRelation getRelationById(Long id) {
        return relationMapper.selectById(id);
    }

    @Override
    public List<Long> getCategoryIdsByCourseId(Long courseId) {
        return relationMapper.selectCategoryIdsByCourseId(courseId);
    }

    @Override
    public List<Long> getCourseIdsByCategoryId(Long categoryId) {
        return relationMapper.selectCourseIdsByCategoryId(categoryId);
    }

    @Override
    @Transactional
    public int batchAddRelations(List<CourseCategoryRelation> relations) {
        if (relations == null || relations.isEmpty()) {
            return 0;
        }
        return relationMapper.batchInsert(relations);
    }

    @Override
    @Transactional
    public int updateCourseRelations(CreateOrUpdateCourseCategoryRelationDTO createOrUpdateCourseCategoryRelationDTO) {
        for (Long courseId : createOrUpdateCourseCategoryRelationDTO.getCourseIds()) {
            // 先删除该课程的所有关联
            int deleteCount = relationMapper.deleteByCourseId(courseId);
        }
        // 如果有新的类别ID，添加新的关联
        if (createOrUpdateCourseCategoryRelationDTO.getCourseIds() != null && !createOrUpdateCourseCategoryRelationDTO.getCourseIds().isEmpty()) {
            List<CourseCategoryRelation> relations = new ArrayList<>();
            for (Long id : createOrUpdateCourseCategoryRelationDTO.getCourseIds()) {
                CourseCategoryRelation relation = new CourseCategoryRelation();
                relation.setCourseId(id);
                relation.setCategoryId(createOrUpdateCourseCategoryRelationDTO.getCategoryId());
                relations.add(relation);
            }
            return  relationMapper.batchInsert(relations);
        }else {
            log.info("没有新的课程ID，不进行关联添加");
            return 0;
        }
    }

    @Override
    public Result<?> selectByCondition(CourseCategoryRelation courseCategory) {
        PageHelper.startPage(courseCategory.getPageNum(), courseCategory.getPageSize());
        List<CourseCategoryRelation> courseCategoryRelations = relationMapper.selectByCondition(courseCategory);
        if (courseCategoryRelations == null || courseCategoryRelations.isEmpty()) {
            return Result.success("没有查询到数据");
        }
        for (CourseCategoryRelation courseCategoryRelation : courseCategoryRelations) {
            Course course = new Course();
            course.setId(courseCategoryRelation.getCourseId());
            course = courseMapper.selectByIdWithTeacher(course.getId());
            courseCategoryRelation.setCourse(course);
        }
        // 封装分页结果到PageData
        PageData<CourseCategoryRelation> pageData = new PageData<>();
        PageInfo<CourseCategoryRelation> pageInfo = new PageInfo<>(courseCategoryRelations);
        pageData.setTotal(pageInfo.getTotal());
        pageData.setData(pageInfo.getList());
        pageData.setPageNum(courseCategory.getPageNum());
        pageData.setPageSize(courseCategory.getPageSize());
        return Result.success(pageData);
    }
}
